package com.yinhe.music.yhmusic.album;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.TabAdapter;
import com.yinhe.music.yhmusic.album.list.AlbumFragment;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.downmanager.DownLoadFragment;
import com.yinhe.music.yhmusic.downmanager.DownloadFinishFragment;
import com.yinhe.music.yhmusic.music.list.MusicListFragment;
import com.yinhe.music.yhmusic.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.yinhe.music.yhmusic.constants.IConstants.NEW_MUSIC_LIST;


/**
 * 标签分类
 *
 * @author 何晓华
 */
public class TabActivity extends BaseServiceActivity {
    @BindView(R.id.viewpager)
    ViewPager mViewPager;
    @BindView(R.id.tabs)
    TabLayout mTab;

    private TabAdapter mAdapter;
    private int type;
    private String[] downTitle = {"已下载", "下载中"};
    private String[] songTitle = {"新歌", "专辑"};

    @Override
    public void initView() {
        if (getIntent() != null) {
            type = getIntent().getIntExtra("type", 0);
        }

        hideToolbar();
        setTabToolbar();
        setTab();
        showQuickControl(true);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_down_load;
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void initPresenter() {

    }

    /**
     * 设置toolbar
     */
    private void setTabToolbar() {
        Toolbar mToolbar = findViewById(R.id.tab_toolbar);

        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            mToolbar.setTitle("");
        }

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setTitle("");
        }
    }

    /**
     * 初始化tab
     */
    private void setTab() {
        ViewUtils.reflexIndicator(mTab, 15);
        mTab.setupWithViewPager(mViewPager);

        if (type == IConstants.DOWNLOAD_ACTIVITY) {
            initDownLoadAdapter();
        } else {
            initNewMusicAdapter();
        }

        mViewPager.setAdapter(mAdapter);
        mViewPager.setBackgroundColor(getResources().getColor(R.color.white));
    }

    /**
     * 初始化下载页面
     */
    private void initDownLoadAdapter() {
        List<Fragment> downFragmentList = new ArrayList<>();
        downFragmentList.add(new DownloadFinishFragment());
        downFragmentList.add(new DownLoadFragment());
        mAdapter = new TabAdapter(getSupportFragmentManager(), downFragmentList, downTitle);
    }

    /**
     * 初始化新歌页面
     */
    private void initNewMusicAdapter() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(MusicListFragment.newInstance(NEW_MUSIC_LIST,null));
        fragmentList.add(AlbumFragment.newInstance(IConstants.NEW_ALBUM_LIST, 0, null));
        mAdapter = new TabAdapter(getSupportFragmentManager(), fragmentList, songTitle);
    }
}
